<?php
require_once("BaseDAO.php");
require_once("../Entities/Category.php");

class CategoryDAO extends BaseDAO
{
	function CreateCategory($item)
	{
		$sql = "insert into Categories(Name ,Description) values ('" . 
			$item->Name . "','" . $item->Description . "');";
		mysql_query($sql);
		$item->Id = mysql_insert_id();
		return $item;
	}
	
	function UpdateCategory($item)
	{
		$sql = "update Categories set Name='" . $item->Name . "',Description='" . 
			$item->Description . "' where Id='" . $item->Id . "';";
		mysql_query($sql);
	}
	
	function DeleteCategory($id)
	{
		$sql = "delete from Categories where Id='" . $id . "';";
		mysql_query($sql);
	}	
	
	function GetById($id)
	{
		$sql = "select * from Categories where Id='" . $id . "';";
		$result = mysql_query($sql);
		if(mysql_num_rows($result) == 1)
		{
			$row = mysql_fetch_assoc($result);
			$item = new Category();
			$item->Id = $row["Id"];
			$item->Name = $row["Name"];
			$item->Description = $row["Description"];
			return $item;
		}
		return null;
	}
	
	function GetAll()
	{
		$sql = "select * from Categories ;";
		$result = mysql_query($sql);
		$list = array();
		while($row = mysql_fetch_assoc($result))
		{
			$item = new Category();
			$item->Id = $row["Id"];
			$item->Name = $row["Name"];
			$item->Description = $row["Description"];
			$list[] = $item;
		}
		return $list;
	}
} 

?>